/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package war;

import ejb.GestoreVideogameBean;
import ejb.Videogame;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet(name = "ListaVideogiochiPerArticolo", urlPatterns = {"/ListaVideogiochiPerArticolo"})
public class ListaVideogiochiPerArticolo extends HttpServlet {
   //  @EJB
    //GestoreVideogameBean g=new GestoreVideogameBean();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
   
     List<Videogame> videogiochi=(List<Videogame>)session.getAttribute("listVideogiochi");
           String[] listaNomi = (String[])session.getAttribute("nomi"); ;
             if(listaNomi==null){ listaNomi=new String[1];
                listaNomi[0]="";
             }
      
         /* String[] videogiochi = {
              "Fifa 13(Xbox360)",
	      "Fifa 13(PC)",
              "Fifa 13(PS3)",
   	      "Wet(PS3)",
              "Wet(Xbox360)"
	  };*/
        try{
          String titolo= (String)request.getParameter("nome");
	
          /*  out.append("<select multiple size='4'  name='videogichi'>");
			
	    for (int i = 0; i < videogiochi.length; i++) {
               if (videogiochi[i].substring(0, titolo.length()).toUpperCase().equals(titolo.toUpperCase()))
                    out.append("<option value='"+videogiochi[i]+"'>" +videogiochi[i]);
               }
			
   	    out.append("</select>");*/
			
	   out.append("<select size='4'  name='idVideogame' >");
           String videogioco="";
           boolean trovato=false;
           int f=0;
   /*        	  if ("start".equals(request.getParameter("actionType")))
	  {
             out.append("<select multiple size='4' name='videogiochi'>");
  	    for(Videogame v:videogiochi){
                 videogioco=v.getNomeEPiattaforma();
                 out.append("<option value='"+videogioco+"'>" +videogioco);
            
	    }
			
	  out.append("</select>");
	 }
                  else{*/
           
           
           
           for(Videogame v:videogiochi){
                videogioco=v.getNomeEPiattaforma();
               if (videogioco.substring(0, titolo.length()).toUpperCase().equals(titolo.toUpperCase())){
                       out.append("<option value='"+v.getId()+"'>" +videogioco);
           
               }
               
            }
   	   out.append("</select>");
                 // }
        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
